#define _CRT_SECURE_NO_WARNINGS 1
#include"Queue.h"


void QueueInit(Queue* q)
{
	assert(q);
	q->phead = NULL;
	q->ptail = NULL;
	q->size = 0;
}

void QueuePush(Queue* q, QDataType data)
{
	assert(q);
	QNode* ret = (QNode*)malloc(sizeof(QNode));
	if (ret == NULL)
	{
		perror("malloc fail");
		return;
	}
	ret->data = data;
	ret->next = NULL;
	if (q->phead == NULL)
		q->phead = q->ptail = ret;
	else
	{
		q->ptail->next = ret;
		q->ptail = ret;
	}
	q->size++;
}

void QueuePop(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	if (q->phead = q->ptail)
	{
		free(q->phead);
		q->phead = q->ptail = NULL;
	}
	else
	{
		QNode* next = q->phead->next;
		free(q->phead);
		q->phead = next;
	}
	q->size--;
}

QDataType QueueFront(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->phead->data;
}

QDataType QueueBack(Queue* q)
{
	assert(q);
	assert(!QueueEmpty(q));
	return q->ptail->data;
}

int QueueSize(Queue* q)
{
	assert(q);
	return q->size;
}
 
bool QueueEmpty(Queue* q)
{
	assert(q);
	return q->phead == NULL && q->ptail == NULL;
}

void QueueDestroy(Queue* q)
{
	assert(q);
	QNode* cur = q->phead;
	while (cur)
	{
		QNode* next = cur->next;
		free(cur);
		cur = next;
	}
	q->phead = q->ptail = NULL;
	q->size = 0;
}
